package org.zfm.org.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.zfm.org.service.IFastFdfsService;
import org.zfm.util.utils.FastFdfsUtil;

import java.io.IOException;

@Service
public class FastFdfsServiceImpl implements IFastFdfsService {
    @Value("${fastdfs.tracker_server.port}")
    private String prefix;
    /**
     * 文件-图片 上传
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @Override
    public String upload(MultipartFile multipartFile) throws IOException {
        String originalFilename = multipartFile.getOriginalFilename();
        String ext_name = originalFilename.substring(originalFilename.indexOf('.')+1);
        String upload = FastFdfsUtil.upload(multipartFile.getBytes(), ext_name);
        return prefix+upload;
    }

    @Override
    public void delete(String path) {
        path = path.substring(prefix.length()+1);
        int indexOf = path.indexOf('/');
        String groupName = path.substring(0,indexOf);
        String ext_name = path.substring(indexOf+1);
        FastFdfsUtil.delete(groupName,ext_name);
    }
}
